﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using CRM.Web.Common;

namespace CRM.Web.MenuManage
{
    public partial class Modify : Common.PageBase
    {
        T_MENU_BLL bll = new T_MENU_BLL();
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            PageHelper.SetPrePageUrl(this.Page);
            PageHelper.SetGoBackUrl(this.Page, btnCancel, StaticConstant.NotBackUrl);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlPARENTMENUIDBinding();
                ddlMENUTYPEBinding();
                ddlMENUSTATEBinding();
                ddlMENUTARGETBinding();
                pageDataInit();
            }
        }

        #region 页面初始化数据绑定


        /// <summary>
        ///	上级菜单编号 下拉列表数据绑定
        /// </summary>
        private void ddlPARENTMENUIDBinding()
        {
            List<T_MENU_Entity> list = bll.GetList(String.Format(" and MenuType = '{0}' ", ((int)MenuType.目录).ToString()));

            ddlPARENTMENUID.DataSource = list;
            ddlPARENTMENUID.DataTextField = "MENUNAME";
            ddlPARENTMENUID.DataValueField = "MENUID";
            ddlPARENTMENUID.DataBind();
            ddlPARENTMENUID.Items.Insert(0, new ListItem("", "0"));
        }

        private void ddlMENUTYPEBinding()
        {
            ddlMENUTYPE.Items.Clear();
            EnumHelper.FillListControl(ddlMENUTYPE, typeof(MenuType));
        }

        private void ddlMENUSTATEBinding()
        {
            ddlMENUSTATE.Items.Clear();
            EnumHelper.FillListControl(ddlMENUSTATE, typeof(Status));

        }

        private void ddlMENUTARGETBinding()
        {
            ddlMENUTARGET.Items.Clear();
            ddlMENUTARGET.Items.Add(new ListItem("frameMain", "frameMain"));
            ddlMENUTARGET.Items.Add(new ListItem("_blank", "_blank"));

        }







        #endregion

        private void pageDataInit()
        {
            string _menuid = String.IsNullOrEmpty(Request.QueryString["MENUID"]) ? "" : Request.QueryString["MENUID"].Trim();

            if (!String.IsNullOrEmpty(_menuid))
            {
                T_MENU_Entity entity = bll.GetEntity("MENUID",_menuid);

                PageHelper.SetControlValue<T_MENU_Entity>(this.Page, entity);
            }


        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string _menuid = String.IsNullOrEmpty(Request.QueryString["MENUID"]) ? "" : Request.QueryString["MENUID"].Trim();

            T_MENU_Entity entity = bll.GetEntity("MENUID", _menuid);

            if (entity != null)
            {
                PageHelper.GetControlValue<T_MENU_Entity>(this.Page, ref entity);
                try
                {

                    long rel = bll.Update(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "修改成功！", UserLoginInfo.FilterBackUrl(PageHelper.GetPrePageUrl(this), "list.aspx"));
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "修改失败！");
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "修改失败！" + ex.Message);
                }
            }
            else
            {
                entity = new T_MENU_Entity();

                PageHelper.GetControlValue<T_MENU_Entity>(this.Page, ref entity);

                try
                {
                    long rel = bll.Insert(entity);
                    if (rel > 0)
                    {
                        PageHelper.ShowMessage(this.Page, "添加成功！", "list.aspx");
                    }
                    else
                    {
                        PageHelper.ShowMessage(this.Page, "添加失败！");
                    }
                }
                catch (Exception ex)
                {
                    
                        PageHelper.ShowMessage(this.Page, "添加失败！" + ex.Message);
                }
            }
        }
    }
}
